package io.renren.modules.yckj.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.yckj.dao.TeamDao;
import io.renren.modules.yckj.dto.TeamDTO;
import io.renren.modules.yckj.entity.TeamEntity;
import io.renren.modules.yckj.service.TeamService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 课题组关系表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2025-04-17
 */
@Service
public class TeamServiceImpl extends CrudServiceImpl<TeamDao, TeamEntity, TeamDTO> implements TeamService {

    @Override
    public QueryWrapper<TeamEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        Object userId = params.get("userId");
        Object creatorId = params.get("creatorId");

        String name = (String) params.get("name");
        String creator = (String) params.get("creator");
        String phone = (String) params.get("phone");

        QueryWrapper<TeamEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.eq(ObjectUtil.isAllNotEmpty(userId), "user_id", userId);
        wrapper.eq(ObjectUtil.isAllNotEmpty(creatorId), "creator_id", creatorId);

        wrapper.like(StrUtil.isNotBlank(name), "name", name);
        wrapper.like(StrUtil.isNotBlank(creator), "creator", creator);
        wrapper.like(StrUtil.isNotBlank(phone), "phone", phone);

        return wrapper;
    }


    @Override
    public PageData<TeamDTO> pageForAdmin(Map<String, Object> params) {
        //分页
        IPage<TeamEntity> page = getPage(params, "id", false);
        //查询
        List<TeamEntity> list = baseDao.pageForAdmin(params);
        return getPageData(list, page.getTotal(), TeamDTO.class);
    }

    @Override
    public List<TeamDTO> getSearchList(Map<String, Object> params) {
        List<TeamEntity> entityList = baseDao.getSearchList(params);
        return ConvertUtils.sourceToTarget(entityList, TeamDTO.class);
    }

    @Override
    public List<TeamDTO> getTeamList(Map<String, Object> params) {
        List<TeamEntity> entityList = baseDao.getTeamList(params);
        return ConvertUtils.sourceToTarget(entityList, TeamDTO.class);
    }

    @Override
    public TeamDTO getTeamByName(Map<String, Object> params) {
        TeamEntity  teamByName = baseDao.getTeamByName(params);
        return ConvertUtils.sourceToTarget(teamByName, TeamDTO.class);
    }

}